package com.example.choupaiapp.main.fragment.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.example.choupaiapp.R;
import com.example.choupaiapp.common.ui.fragment.CommonFragment;
import com.example.choupaiapp.common.util.Utils;
import com.example.choupaiapp.main.activity.home.CameraActivity;
import com.example.choupaiapp.main.activity.home.PicturePreviewActivity;
import com.otaliastudios.cameraview.CameraListener;
import com.otaliastudios.cameraview.CameraOptions;
import com.otaliastudios.cameraview.CameraView;
import com.otaliastudios.cameraview.SessionType;
import com.otaliastudios.cameraview.Size;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by ASUS on 2019/12/9.
 */

public class TakePhotoFragment extends CommonFragment {

    //    @BindView(R.id.camera)
//    public CameraView camera;
    @BindView(R.id.rever)
    ImageView rever;
    @BindView(R.id.take_photo)
    ImageView take_photo;
    Unbinder unbinder;
    public CameraView camera;
    @BindView(R.id.fragment_container)
    FrameLayout fragment_container;
    private String flag;

    public static TakePhotoFragment newInstance(String flag) {
        TakePhotoFragment takePhotoFragment = new TakePhotoFragment();
        takePhotoFragment.flag = flag;
        return takePhotoFragment;
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_take_photo, container, false);
        unbinder = ButterKnife.bind(this, view);

        EventBus.getDefault().register(this);


        return view;
    }

    private void onPicture(byte[] jpeg) {
        Size mCaptureNativeSize = camera.getPictureSize();
        PicturePreviewActivity.setImage(jpeg);
        Intent intent = new Intent(context, PicturePreviewActivity.class);
        intent.putExtra("nativeWidth", mCaptureNativeSize.getWidth());
        intent.putExtra("nativeHeight", mCaptureNativeSize.getHeight());
        intent.putExtra("flag",flag);
        startActivity(intent);

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        if (camera != null) {
            camera.start();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (camera != null) {
            camera.stop();
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (camera != null) {
            camera.destroy();
        }
        EventBus.getDefault().unregister(this);
        unbinder.unbind();
    }

    /**
     * 将播放器从父控件中移除
     */
    private void removePlayerFormParent(View View) {
        if (View != null) {
            ViewParent parent = View.getParent();
            if (parent instanceof FrameLayout) {
                ((FrameLayout) parent).removeView(View);
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void OnMessageEvent(String event) {
        if (event.equals("takephoto")) {
            View view = CameraActivity.view;
            camera = view.findViewById(R.id.camera);
            camera.setSessionType(SessionType.PICTURE);
            camera.setLifecycleOwner(this);
            camera.addCameraListener(new CameraListener() {
                public void onCameraOpened(CameraOptions options) {
                }

                public void onPictureTaken(byte[] jpeg) {
                    onPicture(jpeg);
                }

                @Override
                public void onVideoTaken(File video) {
                    super.onVideoTaken(video);
                }
            });


            take_photo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (Utils.isDoubleClick()) {
                        return;
                    }
                    camera.capturePicture();
                }
            });

            rever.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (Utils.isDoubleClick()) {
                        return;
                    }
                    camera.toggleFacing();
                }
            });

            removePlayerFormParent(view);
            fragment_container.addView(view);

            camera.start();
        }

    }
}
